package com.hjx.movie.core.system.service.admin.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjx.movie.common.utils.ResultType;
import com.hjx.movie.common.utils.exceptions.CustomException;
import com.hjx.movie.core.system.service.admin.IRoleService;
import com.hjx.movie.core.util.CombineBeansUtil;
import com.hjx.movie.core.util.cache.CacheKey;
import com.hjx.movie.core.util.cache.CacheUtil;
import com.hjx.movie.persistence.system.mapper.IvyRoleMapper;
import com.hjx.movie.persistence.system.model.IvyRole;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2021/1/23 - 18:35
 */
@Slf4j
@Service
@Transactional
@CacheConfig(cacheNames = "IvyMovie:SpringCache:RoleCache",keyGenerator = "cacheKeyGenerator")
public class RoleService extends ServiceImpl<IvyRoleMapper, IvyRole> implements IRoleService {

    @Resource
    private CacheUtil<IvyRole> cacheUtil;
    public static final String ROLE_KEY = CacheUtil.cacheNameFormat(CacheKey.SYSTEM.getName(),CacheKey.ROLE.getName());
    public static final String ROLE_CACHE_DATA = CacheUtil.cacheNameFormat(CacheKey.SYSTEM.getName(),CacheKey.ROLE.getName(),CacheKey.PRE_SAVE_ROLE.getName());



    @Override
    public boolean addRole(IvyRole role) {
        if (save(role)){
            cacheUtil.getOpsForHash().put(ROLE_CACHE_DATA,role.getRoleId().toString(),role);
            return true;
        }
        return false;
    }

    @Override
    public boolean updateRole(IvyRole role) {
        Assert.notNull(role.getRoleId(),"角色标识不能为空");
        if (updateById(role)){
            IvyRole r = cacheUtil.getOpsForHash().get(ROLE_CACHE_DATA, role.getRoleId().toString());
            Assert.notNull(r,"查询不到角色");
            CombineBeansUtil.combineBean(role,r);
//            r.setUpdateTime(LocalDateTime.now());
            cacheUtil.getOpsForHash().put(ROLE_CACHE_DATA,role.getRoleId().toString(),r);
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteRole(Long roleId) {
        if (removeById(roleId)){
            if(cacheUtil.getOpsForHash().delete(ROLE_CACHE_DATA, roleId.toString())>0){
                return true;
            }else {
                throw new CustomException(ResultType.SYSTEM_ERROR,"删除失败");
            }
        }
        return false;
    }

    @Override
    public List<IvyRole> findAllRole() {
        return cacheUtil.getCacheList(ROLE_CACHE_DATA);
    }

    @Override
    public List<IvyRole> queryRoleLike(String roleLike) {
        List<IvyRole> cacheList = cacheUtil.getCacheList(ROLE_CACHE_DATA);
        return cacheList.stream().filter(
                item->item.getRoleCode().contains(roleLike)
                || item.getRoleInfo().contains(roleLike)
                || item.getRoleName().contains(roleLike)
        ).collect(Collectors.toList());
    }

}
